package com.framework.modules.appindex;

import com.framework.entity.or.OrAppAdv;
import com.framework.exception.util.RespResult;
import com.framework.modules.oss.cloud.OSSFactory;
import com.framework.modules.sys.controller.AbstractController;
import com.framework.page.PageUtils;
import com.framework.service.OrAppAdvService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Map;

/**
 * @author wq
 */
@RestController
@RequestMapping("app/adv")
public class OrAppAdvController  extends AbstractController {

    @Autowired
    private OrAppAdvService orAppAdvService;

    @RequestMapping("list")
    public RespResult list(@RequestParam("params")Map<String,Object> params){
        params.put("deptId",getDeptId());
        PageUtils page=orAppAdvService.queryPage(params);
        return RespResult.ok().put("page",page);
    }

    @RequestMapping("getById/{id}")
    public RespResult getById(@PathVariable Integer id){
        return RespResult.ok().put("adv",orAppAdvService.selectById(id));
    }

    @RequestMapping("save")
    public RespResult save(@RequestBody OrAppAdv orAppAdv){
        orAppAdv.setDeptId(getDeptId());
        orAppAdvService.save(orAppAdv);
        return RespResult.ok();
    }

    @RequestMapping("update")
    public RespResult update(@RequestBody OrAppAdv orAppAdv){
        orAppAdvService.updateById(orAppAdv);
        return RespResult.ok();
    }

    /**
     * 上传
     * @param file
     * @return
     * @throws IOException
     */
    @RequestMapping("upload")
    public RespResult upload(@RequestParam("file") MultipartFile file) throws IOException {
        //上传轮播图
        String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
        String bannerUrl = OSSFactory.build().uploadSuffix(file.getBytes(), suffix, "adv");
        return RespResult.ok().put("url", bannerUrl);
    }


}
